#include <iostream>
#include <future>
#include <chrono>
#include <thread>

int Add(int x, int y)
{
    std::cout << "加法！！！" << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(5));
    std::cout << "加法！！！" << std::endl;
    return x + y;
}

int main()
{
    // std::launch::async  表明函数会在自己创建的线程上运行
    // std::launch::deferred 该函数会被延迟调用，直到在future上调用get()
    std::cout << "------1------" << std::endl;
    std::future<int> result_future = std::async(std::launch::deferred, Add, 10, 11);
    std::this_thread::sleep_for(std::chrono::seconds(1));

    std::cout << "------2------" << std::endl;
    int result = result_future.get();

    std::cout << "------3------" << std::endl;
    std::cout << "相加的结果为：" << result << std::endl;

    return 0;
}
